<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px">
            <el-form-item label="请输入新密码" prop="pwd">
                <el-input show-password v-model="ruleForm.pwd"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :loading="loading" @click="sub('ruleForm')">确定</el-button>
                <el-button @click="onCancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { apiEdit } from "@/api/admin.js";
export default {
    name: "admin_user_index_edit_pwd",
    props: {
        userId: {
            type: Number,
            required: true
        }
    },
    data() {
        return {
            ruleForm: {
                pwd: "",
            },
            rules: {
                pwd: [{ required: true, message: "请输入新密码", trigger: "change" }]
            },
            loading: false,
        }
    },
    methods: {
        // 确定
        sub(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    // if (this.ruleForm.pwd.length < 6) {
                    //     this.$message({ message: "密码不能少于6位", type: "error" });
                    //     return;
                    // }
                    this.loading = true;
                    apiEdit({ id: this.userId, password: this.ruleForm.pwd }).then((res) => {
                        this.$message({ message: "操作成功", type: "success" });
                        this.loading = false;
                        this.$emit("onSubmit");
                    }).catch((err) => {
                        this.loading = false;
                    });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        // 取消
        onCancel() {
            this.loading = false;
            this.$emit("onCancel");
        },
        // 重置
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    },
}
</script>

<style></style>